﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class EstimatePartListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void LoadEstimatePartListByID(int companyid, int Estimateid)
        {
            hfEstimateId.Value = Estimateid.ToString();
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select pp.Estimateproductid, pp.Estimateid, pp.productid, pp.quantity, i.productname, i.unitsinstock, i.ModelNo from rs_tblEstimatePart as pp left join rs_tblInventory i on pp.ProductId=i.productid left join rs_tblEstimate p on pp.Estimateid=p.Estimateid where pp.Estimateid=@Estimateid and pp.companyid=@companyid order by productname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@Estimateid";
            param.Value = Estimateid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvEstimatePartList.DataSource = ds;
            gvEstimatePartList.DataBind();
            myConnection.Close();
        }
        protected void gvProduct_Add(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int Estimateid = hfEstimateId.Value == "" ? 0 : Convert.ToInt32(hfEstimateId.Value);
                Response.Redirect("~/RS/SBM/RSShoppingPart.aspx?isEstimate=1&action=add&Estimateid=" + Estimateid);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void gvProduct_Edit(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int productid = Convert.ToInt32(e.CommandArgument.ToString());
                ucModalProductControl.ShowModal(companyid, productid, false);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void gvProduct_Remove(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int Estimateproductid = Convert.ToInt32(e.CommandArgument.ToString());
                if (companyid > 0 && Estimateproductid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@Estimateproductid", SqlDbType.Int, 0, ParameterDirection.Input, Estimateproductid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblEstimatePart WHERE Estimateproductid=@Estimateproductid AND companyid=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Product removed successfully!", Request.RawUrl, true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }


    }

    
}